use curl::easy::Easy;

const URL: &'static str = "https://projecteuler.net/project/resources/p022_names.txt";

/// [Names scores](https://projecteuler.net/problem=22)
fn main() {
    let mut cont = Vec::new();
    {
        let mut easy = Easy::new();
        easy.url(URL).unwrap();
        let mut transfer = easy.transfer();
        transfer
            .write_function(|data| {
                cont.extend_from_slice(data);
                Ok(data.len())
            })
            .unwrap();
        transfer.perform().unwrap();
    }

    let mut name_vec = String::from_utf8(cont)
        .unwrap()
        .split(",")
        .map(|s| s.trim_matches('"').to_owned())
        .collect::<Vec<_>>();
    name_vec.sort();

    let sum = name_vec
        .iter()
        .enumerate()
        .map(|(i, s)| s.chars().fold(0, |sum, x| sum + x.to_digit(36).unwrap() - 9) * (i + 1) as u32)
        .fold(0, |sum, x| sum + x);
    println!("{}", sum);
}
